package com.itheima.demo05;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/7/13 18:37
 */
public class Ticket_002 implements Runnable {
    private int ticket1 = 100;

    @Override
    public void run() {
        while (true) {
            if (method()) {
                break;
            }
        }

    }

    private synchronized boolean method() {
        if (ticket1 <= 0) {
            return true;
        } else {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticket1--;
            System.out.println(Thread.currentThread().getName() +
                    "正在售票,还剩下" + ticket1 + "张票!");
        }
        return false;
    }
}
